function ccR_Priest_Shadow_Word_Pain()
	if ccrSpellCanAttack(SHADOW_WORD_PAIN)
	and not ccrTargetHasMyDebuff(SHADOW_WORD_PAIN)
	and (
			(vPriest_Shadow_Word_Pain == nil or (GetTime()-vPriest_Shadow_Word_Pain) > 1.5) 
			or
			(vPriest_Shadow_Word_Pain_GUID == nil or (UnitGUID("target") ~= vPriest_Shadow_Word_Pain_GUID))
		)
	then
		ccrCastAttack(SHADOW_WORD_PAIN)
		vPriest_Shadow_Word_Pain_Time = GetTime()
		vPriest_Shadow_Word_Pain_GUID = UnitGUID("target")
	end
end

function ccR_Priest_Shadow_Word_Pain_All()
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Priest_" .. specNumber .. "_UseShadowWordPainAll"]
	local vManaMinimum = ccRS["Priest_" .. specNumber .. "_ShadowWordPainAllMana"]
	if vUseAbility
	and ccrPlayerHasSpell(SHADOW_WORD_PAIN)
	and ccrSpellIsReady(SHADOW_WORD_PAIN)
	and ccrPlayerMana() >= vManaMinimum
	then
		local x
		if # tblPGetNearbyEnemies > 0 then
			for x = 0, # tblPGetNearbyEnemies do
				local obj = tblPGetNearbyEnemies[x]
				local vSpell = {GetSpellInfo(SHADOW_WORD_PAIN)}
				if obj
				and obj:Exists()
				and (obj:IsTappedByPlayer() or (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName()))
				and obj:GetAura(vSpell[1]) == nil
				and obj:InLineOfSight()
				and ccrSpellCanAttack(SHADOW_WORD_PAIN, obj)
				and not ccrIgnoreNPCWithAura(obj)
				then
					obj:CastSpellByName(GetSpellInfo(SHADOW_WORD_PAIN))
					return
				end
			end
		end
	end
	return
end

function ccR_Priest_Power_Word_Shield(specNumber)
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Priest_" .. specNumber .. "_UsePowerWordShield"]

	if vUseAbility
	and ccrPlayerHasSpell(POWER_WORD_SHIELD)
	and ccrSpellIsReady(POWER_WORD_SHIELD)
	and not ccrPlayerHasBuff(POWER_WORD_SHIELD)
	then
		local x
		if # tblPGetMeleeEnemies > 0 then
			for x = 0, # tblPGetMeleeEnemies do
				local obj = tblPGetMeleeEnemies[x]
				if obj
				and obj:Exists()
				and (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName())
				then
					ccrCast(POWER_WORD_SHIELD, "Player")
					return
				end
			end
		end
	end
end

function ccR_Priest_Psychic_Scream(specNumber)
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Priest_" .. specNumber .. "_UsePsychicScream"]

	if vUseAbility
	and ccrPlayerHasSpell(PSYCHIC_SCREAM)
	and ccrSpellIsReady(PSYCHIC_SCREAM)
	then
		local x
		if # tblPGetMeleeEnemies > 0 then
			for x = 0, # tblPGetMeleeEnemies do
				local obj = tblPGetMeleeEnemies[x]
				if obj
				and obj:Exists()
				and (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName())
				and obj:InLineOfSight()
				then
					ccrCast(PSYCHIC_SCREAM)
					return
				end
			end
		end
	end
end

function ccR_Priest_Shadow_Mind_Flay()
	if ccrPlayerHasSpell(MIND_FLAY)
	and ccrSpellCanAttack(MIND_FLAY)
	and not ccrPlayerIsCastingOrChanneling()
	and not ccrPlayerIsMoving()
	then
		ccrCastAttack(MIND_FLAY)
		return
	end
end

function ccR_Priest_Mind_Blast()
	if ccrPlayerHasSpell(MIND_BLAST)
	and ccrSpellIsReady(MIND_BLAST)
	and ccrSpellCanAttack(MIND_BLAST)
	and Target and Target:Exists() and Player:IsFacing(Target)
	and not ccrPlayerIsMoving()
	then
		ccrCastAttack(MIND_BLAST)
		return
	end
end

function ccR_Priest_Devouring_Plague()
	local vOrbCount = 1
	-- Bosses before 84 die way too fast
	-- to earn 3 Orbs
	if ccrPlayerLevel() < 84 then
		vOrbCount = 1
	else
		vOrbCount = 3
	end	

	if ccrPlayerHasSpell(DEVOURING_PLAGUE)
	and (
			(not ccrTargetIsBoss() and ccrOrbs() > 0) 
			or
			(ccrTargetIsBoss() and ccrOrbs() >= vOrbCount)
		)
	and ccrSpellCanAttack(DEVOURING_PLAGUE)
	then
		ccrCastAttack(DEVOURING_PLAGUE)
		return
	end
end

function ccR_Priest_Shadowform()
	if ccrPlayerHasSpell(SHADOWFORM)
	and not ccrPlayerHasBuff(SHADOWFORM)
	and ccrSpellIsReady(SHADOWFORM)
	then
		ccrCast(SHADOWFORM)
		return
	end
	return
end

function ccR_Priest_Inner_Fire()
	if not ccrPlayerHasBuff(INNER_FIRE)
	and ccrSpellIsReady(INNER_FIRE)
	then
		ccrCast(INNER_FIRE)
		return
	end
	return
end

function ccR_Priest_Power_Word_Fortitude()
	if ccrPlayerHasSpell(POWER_WORD_FORTITUDE)
	and not ccrPlayerHasBuff(POWER_WORD_FORTITUDE)
	and ccrSpellIsReady(POWER_WORD_FORTITUDE)
	then
		ccrCast(POWER_WORD_FORTITUDE)
		return
	end
	return
end

function ccR_Priest_Vampiric_Touch()
	if ccrPlayerHasSpell(VAMPIRIC_TOUCH)
	and ccrSpellCanAttack(VAMPIRIC_TOUCH)
	and not ccrTargetHasMyDebuff(VAMPIRIC_TOUCH)
	and (vPriest_Last_Vampiric_Touch_Cast == nil or (GetTime()-vPriest_Last_Vampiric_Touch_Cast > 2))
	and not ccrPlayerIsMoving()
	then
		ccrCastAttack(VAMPIRIC_TOUCH)
		vPriest_Last_Vampiric_Touch_Cast = GetTime()
		return
	end
	return
end

function ccR_Priest_Vampiric_Touch_All()
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Priest_" .. specNumber .. "_UseVampiricTouchAll"]
	if vUseAbility
	and ccrPlayerHasSpell(VAMPIRIC_TOUCH)
	and ccrSpellIsReady(VAMPIRIC_TOUCH)
	and (vPriest_Last_Vampiric_Touch_Cast == nil or (GetTime()-vPriest_Last_Vampiric_Touch_Cast > 2))
	and not ccrPlayerIsMoving()
	then
		local x
		if # tblPGetNearbyEnemies > 0 then
			for x = 0, # tblPGetNearbyEnemies do
				local obj = tblPGetNearbyEnemies[x]
				local vSpell = {GetSpellInfo(VAMPIRIC_TOUCH)}
				if obj
				and obj:Exists()
				and (obj:IsTappedByPlayer() or (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName()))
				and obj:GetAura(vSpell[1]) == nil
				and obj:InLineOfSight()
				and ccrSpellCanAttack(VAMPIRIC_TOUCH, obj)
				and not ccrIgnoreNPCWithAura(obj)
				then
					obj:CastSpellByName(GetSpellInfo(VAMPIRIC_TOUCH))
					vPriest_Last_Vampiric_Touch_Cast = GetTime()
					return
				end
			end
		end
	end
end

function ccR_Priest_Shadowfiend(specNumber)
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Priest_" .. specNumber .. "_UseShadowfiend"]
	local vMana = ccRS["Priest_" .. specNumber .. "_ShadowfiendMP"]
	if vUseAbility
	and ccrPlayerHasSpell(SHADOWFIEND)
	and ccrSpellIsReady(SHADOWFIEND)
	and ccrPlayerMana() <= vMana
	and ccrSpellCanAttack(SHADOWFIEND)
	then
		ccrCastAttack(SHADOWFIEND)
		return
	end
	return
end

function ccR_Priest_Shadow_Word_Death()
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Priest_" .. specNumber .. "_UseShadowWordDeathAnyone"]
	if not vUseAbility then
		if ccrPlayerHasSpell(SHADOW_WORD_DEATH)
		and ccrSpellIsReady(SHADOW_WORD_DEATH)
		and ccrSpellCanAttack(SHADOW_WORD_DEATH)
		and ccrTargetHealth() <= 20
		then
			if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
			ccrCastAttack(SHADOW_WORD_DEATH)
			return
		end
	end
end

function ccR_Priest_Shadow_Word_Death_Anyone()
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Priest_" .. specNumber .. "_UseShadowWordDeathAnyone"]
	if vUseAbility
	and ccrPlayerHasSpell(SHADOW_WORD_DEATH)
	and not onCD(SHADOW_WORD_DEATH)
	then
		local x
		local vCurrentTarget
		if # tblPGetNearbyEnemies > 0 then
			for x = 0, # tblPGetNearbyEnemies do
				local obj = tblPGetNearbyEnemies[x]
				if obj
				and obj:Exists()
				and (obj:IsTappedByPlayer() or (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName()))
				and obj:InLineOfSight()
				and ccrUnitHealth(obj) <= 20
				then
					if ccrPlayerIsCastingOrChanneling() then SpellStopCasting() end
					if Target
					and Target:Exists()
					and obj:GetGUID() ~= Target:GetGUID()
					then
						vCurrentTarget = Target
						obj:Target()
						obj:CastSpellByName(GetSpellInfo(SHADOW_WORD_DEATH))
					else
						obj:CastSpellByName(GetSpellInfo(SHADOW_WORD_DEATH))
					end
				end
			end
		end
		if vCurrentTarget ~= nil then vCurrentTarget:Target() end

	end	
end

function ccR_Priest_Silence(specNumber)
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Priest_" .. specNumber .. "_UseSilence"]
	if ccrPlayerHasSpell(SILENCE)
	and vUseAbility
	and ccrSpellIsReady(SILENCE)
	and ccrTargetIsCastingOrChanneling()
	and ccrTargetIsInteruptable()
	then
		if ccrTargetIsCasting()
		and ccrTargetHasLeftOnCast(1)
		then
			ccrCast(SILENCE)
			return
		end

		if ccrTargetIsChanneling() then
			ccrCast(SILENCE)
			return
		end
	end
	return
end

function ccR_Priest_Silence_Anyone(specNumber)
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Priest_" .. specNumber .. "_UseSilenceOnAnyone"]
	if vUseAbility
	and ccrPlayerHasSpell(SILENCE)
	and ccrSpellIsReady(SILENCE)
	then
		local x
		if # tblPGetNearbyEnemies > 0 then
			for x = 0, # tblPGetNearbyEnemies do
				local obj = tblPGetNearbyEnemies[x]
				if obj
				and obj:Exists()
				and (obj:GetTarget():Exists() and obj:GetTarget():GetName() == ccrPlayerName())
				and obj:InLineOfSight()
				and ccrUnitIsCastingOrChanneling(obj)
				and ccrUnitIsInteruptable(obj)
				then
					if ccrUnitIsCasting(obj)
					and ccrUnitHasLeftOnCast(obj, 1)
					then
						obj:CastSpellByName(GetSpellInfo(SILENCE))
						return
					end
					if ccrUnitIsChanneling(obj) then
						obj:CastSpellByName(GetSpellInfo(SILENCE))
						return
					end									
				end
			end
		end	
	end
end

vPriest_Shadow_Last_Mind_Sear_Check = nil
function ccR_Priest_Mind_Sear()
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Priest_" .. specNumber .. "_UseMindSear"]
	local vHostilesAroundTank = ccRS["Priest_" .. specNumber .. "_MindSearCount"]
	if ccrPlayerHasSpell(MIND_SEAR)
	and vUseAbility
	and (GetNumGroupMembers() ~= 0 and (PlayerIsInDungeon() or PlayerIsInRaid()))
	and ccrSpellIsReady(MIND_SEAR)
	and not ccrPlayerIsMoving()
	and not ccrPlayerIsCastingOrChanneling()
	and (vPriest_Shadow_Last_Mind_Sear_Check == nil or (GetTime()-vPriest_Shadow_Last_Mind_Sear_Check > 2.0))
	then
		local vGroupType
		local vTank
		local objTank
		if IsInGroup() then vGroupType = "party" end;
		if IsInRaid() then vGroupType = "raid" end;
		-- Search for a tank
		for i = 0, GetNumGroupMembers() do
			if ccrUnitIsTank(vGroupType .. tostring(i)) then vTank = vGroupType .. tostring(i) end;
		end
		-- If we've found one and in range
		if vTank ~= nil
		and UnitInRange(vTank)
		then
			-- See how many enemies are around him
			objTank = GetObjectFromUnitID(vTank)
			local vHostileCount = 0
			for x = 1, # ccrObjectListTable do
				local obj = ccrObjectListTable[x]
				if obj
				and obj:Exists()
				and (obj:GetType() == 3 or obj:GetType() == 4)
				and not obj:IsDead()
				and not tContains(tblObjectListIgnore, obj:GetName())
				and not tContains(tblObjectListIgnore, obj:GetCreatureInfo())
				and obj:GetDistance(objTank) <= 12
				and obj:IsAttackable()
				then
					vHostileCount = vHostileCount + 1
				end
			end
			-- Collected hostile count, See if it's worth doing Mind Sear
			if vHostileCount >= vHostilesAroundTank then
				-- Target the tank and cast Mind Sear
				objTank:CastSpellByName(GetSpellInfo(MIND_SEAR))
			end
		end
		vPriest_Shadow_Last_Mind_Sear_Check = GetTime()
	end
end

function ccR_Priest_Prayer_Of_Mending(specNumber)
	if specNumber == nil then specNumber = ccrSpec() end
	local vUseAbility = ccRS["Priest_" .. specNumber .. "_UsePrayerOfMending"]
	local vHealth = ccRS["Priest_" .. specNumber .. "_PrayerOfMendingHP"]
	if vUseAbility
	and ccrPlayerHasSpell(PRAYER_OF_MENDING)
	and ccrSpellIsReady(PRAYER_OF_MENDING)
	and ccrPlayerHealth() <= vHealth
	then
		ccrCast(PRAYER_OF_MENDING, "Player")
		return
	end
end